package com.canbot.u05.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Comparator;

/**
 * Created by zmp on 2017/7/29.
 */

public class MyBaseFrameView extends View {

        private int width;

        private int height;

        private Bitmap mBitmap;

        private String mFileDir;

        private String[] mFileNames;

        private int index;

        private Bitmap bitmap;

        public MyBaseFrameView(Context context) {
                super(context);
        }

        public MyBaseFrameView(Context context, @Nullable AttributeSet attrs) {
                super(context, attrs);
        }

        public MyBaseFrameView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
                super(context, attrs, defStyleAttr);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                super.onSizeChanged(w, h, oldw, oldh);
                width = getMeasuredWidth();
                height = getMeasuredHeight();
        }

//        public void setBimap(Bitmap bitmap) {
//                if (width != 0 && height != 0) {
//                        scaledBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
//                        invalidate();
//                }
//        }

        @Override
        protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                if (mBitmap == null) {
                        return;
                }
                canvas.drawBitmap(mBitmap, (width - mBitmap.getWidth()) / 2, (height - mBitmap.getHeight()) / 2, null);
                mBitmap.recycle();
                mBitmap = null;
        }

        public void setBitmapDir(String dir) {
                File file = new File(dir);
                if (!file.exists() || file.isFile()) {
                        return;
                }
                this.mFileDir = dir;
                this.mFileNames = file.list(new FilenameFilter() {
                        @Override
                        public boolean accept(File dir, String filename) {
                                return filename.endsWith(".png") || filename.endsWith(".jpg");
                        }
                });
                Arrays.sort(mFileNames, new Comparator<String>() {
                        @Override
                        public int compare(String lhs, String rhs) {
                                return lhs.compareTo(rhs);
                        }
                });
                onResme();
        }

        public void onResme() {
                Log.e("onResme", "onResme");
                if (mFileNames == null || mFileNames.length == 0) {
                        return;
                }
                mBitmap = BitmapFactory.decodeFile(mFileDir + mFileNames[index]);
                invalidate();
                index++;
                if (index > mFileNames.length - 1) {
                        index = 0;
                }
                removeCallbacks(runnable);
                postDelayed(runnable, 80);

        }

        public void onPause() {
                removeCallbacks(runnable);
        }

        Runnable runnable = new Runnable() {

                @Override
                public void run() {
                        if (mFileNames == null || mFileNames.length == 0) {
                                return;
                        }
                        mBitmap = BitmapFactory.decodeFile(mFileDir + mFileNames[index]);
                        invalidate();
                        index++;
                        if (index > mFileNames.length - 1) {
                                index = 0;
                        }
                        postDelayed(this, 80);
                }
        };
}
